import React from 'react'
import Form from '../../../share/Form'
import PropTypes from 'prop-types'
import uuid from 'uuid'
import * as FormUtil from '../../../public/js/formUtil'
import * as AlertUtil from '../../../public/js/alertUtil'

/**
 * 重置密码
 */
class ResetPwd extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            formId: uuid.v4()
        }
    }

    componentWillReceiveProps(nextProps) {
        let form =$('#' + this.state.formId)
        FormUtil.loadJsonDataToForm(form, nextProps.formData)
    }

    resetPwd() {
        let url = domainName + '/users/' + this.props.formData.id + '/password', data = $('#' + this.state.formId).serializeObject()
        FormUtil.submit(url, data, (response)=>{
            AlertUtil.alert('提示',"重置密码成功!",()=>{
                $('#' + this.props.modalId).modal('hide')
            })
        },(error) => {
            AlertUtil.alert('提示',"保存失败,请联系管理员!")
        })
    }

    cancel() {
        $('#' + this.props.modalId).modal('hide')
    }

    render() {
        return (
            <div>
                <div className="modal fade" id={this.props.modalId} tabIndex="-1" role="dialog">
                    <div className="modal-dialog modal-lg">
                        <div className="modal-content">
                            <div className="modal-header">
                                <h4 className="modal-title">重置密码</h4>
                                <button type="button" className="close" data-dismiss="modal" aria-hidden="true">×</button>
                            </div>
                            <div className="modal-body">
                                <Form formId = {this.state.formId} saveHandle={this.resetPwd.bind(this)} cancelHandle={this.cancel.bind(this)}>
                                    <input type="hidden" name="id"/>
                                    <div className="form-group row">
                                        <label className="col-sm-2 text-right control-label col-form-label">密码<span className="text-danger">*</span></label>
                                        <div className="col-sm-10">
                                            <input type="password" name="newPassword" className="form-control" placeholder="密码" required maxLength="50"/>
                                        </div>
                                    </div>
                                </Form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        )
    }
}

ResetPwd.propTypes = {
    formData : PropTypes.object.isRequired,
    modalId : PropTypes.string.isRequired
}

export default ResetPwd

